package cn.itsub.demo.game;

import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import cn.itsub.sodo.Node;
import cn.itsub.sodo.Scene;
import cn.itsub.sodo.Sprite;
import cn.itsub.sodo.action.interval.MoveTo;
import cn.itsub.sodo.audio.SoundEngine;
import cn.itsub.sodo.core.Director;
import cn.itsub.sodo.core.types.Size;
import cn.itsub.sodo.core.types.Vec2;
import cn.itsub.sodo.external.ProgressBar;
import cn.itsub.sodo.timer.HiScheduler;
import cn.itsub.sodo.timer.UpdateCallback;

public class HelloScene extends Scene {
	final Director dir = Director.getInstance();
	
	public HelloScene() {
		init();
	}
	
	public void init(){
		
		Node a = new Node();
		a.setPosition(500, 100);
		
		Node b = new Node();
		b.setPosition(100, 100);

		Node c = new Node();
		c.setPosition(-200, 100);
		
		a.addChild(b);
		b.addChild(c);
		
		this.addChild(a);
		
		System.out.println(c.getWorldPosition());
		
		final Scene scene = this;
		
		new SoundEngine().playBackgroundMusic("music/QQ三国 - 青鸟鸣月.mp3", true);
		
		final Sprite bg2 = new Sprite("横版地图5.png");
		bg2.getPosition().x = bg2.getSize().width/2;
		bg2.getPosition().y = dir.view.getDesignSize().height/2;
		bg2.setPriority(-1);
		this.addChild(bg2);
		bg2.setScaleY(bg2.getTexture().getHeight()/Director.getInstance().getView().getFrame().getHeight());
		
		final Sprite bg3 = new Sprite("横版地图5_2.png");
		bg3.getPosition().x = 0;
		bg3.getPosition().y = 0;
		bg3.setScale(1.5f);
		bg3.setPriority(-2);
		this.addChild(bg3);
		
		final Hero shana = new Hero();
		shana.setPositionX(400);
		shana.setPositionY(180);
		shana.setScale(2.0f);
		shana.setAnchorX(0.5f);
		shana.setAnchorY(0.5f);
		shana.setPriority(1);
		shana.setRotation(0);
		shana.setOpacity(0.5f);
		this.addChild(shana,10);
		
		shana.idle();
		
		
		
		ProgressBar bar = new ProgressBar("HelloWorld.png");
		bar.setMid(new Vec2(0, 0.5f));
		bar.setRate(new Vec2(1, 0));
		bar.setProgress(50);
		bar.setPosition(200, 200);
		this.addChild(bar,10);
		
		final List<Hero> hs = new ArrayList<Hero>();
		

		final Frame frame = Director.view.getFrame();
		frame.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				switch (e.getKeyCode()) {
				case KeyEvent.VK_UP:
					Director.view.setFullScreenWindow(true);
					break;
				case KeyEvent.VK_DOWN:
					Director.view.setFullScreenWindow(false);
					break;
				case KeyEvent.VK_RIGHT:
					shana.setRotation(shana.getRotation()+2);
					scene.setPositionX(scene.getPosition().x+2);
					break;
				case KeyEvent.VK_LEFT:
					shana.setRotation(shana.getRotation()-2);
					scene.setPositionX(scene.getPosition().x-2);
					break;
				case KeyEvent.VK_S:
					dir.pause();
					break;
				case KeyEvent.VK_R:
					dir.resume();
					break;
				case KeyEvent.VK_X:
					for(Hero ho : hs){
						MoveTo mv = MoveTo.action(1f, (float)Math.random()*800, (float)Math.random()*600);
						ho.runAction(mv);
					}
					shana.attack();
					System.out.println(shana.getWorldPosition());
					break;
				case KeyEvent.VK_Z:
					MoveTo mv = MoveTo.action(1f, (float)Math.random()*800, (float)Math.random()*600);
					shana.runAction(mv);
					
					break;
				default:
					break;
				}
			}
		});
		
		
		
		System.out.println("------------ 开始添加精灵 ------------");
		
		for(int i=1;i<=30;i++){
			Hero hero = createHero();
			scene.addChild(hero);
			hero.run();
			hs.add(hero);
		}
		
		
		//创建5000辆坦克
		Node node2 = new Node();
		for(int i=1;i<=5;i++){
			Sprite sp = new Sprite("Tank_D.png");
			sp.setPosition((float)Math.random()*800,(float) Math.random()*600);
			node2.addChild(sp);
		}
		this.addChild(node2, -1);
		
		System.out.println("------------ 精灵就绪 ------------");
		
		//创建调度任务，每隔1000毫秒执行一次
		HiScheduler.getInstance().schedule(new UpdateCallback() {
			public void update(float d) {
				Hero ho = createHero();
				shana.addChild(ho);
				ho.run();
				System.out.println(scene.getChildren().size());
			}
		}, this, 1.0f, paused);
		
	}
	
	Hero createHero(){
		Director director = Director.getInstance();
		Size design = director.getView().getDesignSize();
		float scale = 1.0f;
		final Hero hero = new Hero();
		hero.setPosition((float)Math.random()*design.width, (float)Math.random()*design.height);
		hero.setScaleX((Math.random()>0.5)? scale:-scale);
		hero.setScaleY(scale);
		hero.setRotation((int) (Math.random()*180));
		return hero;
	}
	
}
